package com.crm.service.system.impl;

import com.crm.mapper.BusinessMapper;
import com.crm.mapper.ReportMapper;
import com.crm.mapper.system.HomePageMapper;
import com.crm.service.ReportService;
import com.crm.vo.BusinessChangeVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private HomePageMapper homePageMapper;


    @Override
    public List<BusinessChangeVO> businessChangeStatistics(String beginCreateTime, String endCreateTime) {

        //获取总的商机数量
        int all = businessMapper.getAll(beginCreateTime,endCreateTime);
        System.err.println("商机all:" + all);
        //获取转换商机数据
        List<BusinessChangeVO> volist = businessMapper.getVOlist(beginCreateTime,endCreateTime);
        System.err.println(volist);

        //计算转化百分比
        List<BusinessChangeVO> collect = volist.stream().map(vo -> {
            vo.setRadio(getRadio(all, vo.getNum()));
            return vo;
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 线索龙虎榜
     * @param beginCreateTime
     * @param endCreateTime
     * @param
     * @return
     */
    @Override
    public List<BusinessChangeVO> salesStatistic(String beginCreateTime, String endCreateTime) {
        //获取总的线索数量
        int all = businessMapper.getCluesNums(beginCreateTime,endCreateTime);
        System.err.println("线索all:" + all);
        //获取转换线索数据
        List<BusinessChangeVO> volist = businessMapper.getCluelist(beginCreateTime,endCreateTime);
        System.err.println(volist);

        List<BusinessChangeVO> collect = volist.stream().map(vo -> {
            vo.setRadio(getRadio(all, vo.getNum()));
            return vo;
        }).collect(Collectors.toList());

        return collect;
    }

    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }
}
